package com.doradance.app.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.doradance.R;
import com.doradance.app.appactivity.search.SearchActivity;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;

public class ModifyNicknameActivity extends BaseActivity {
    String nickname;
    public static final int NICKNAMECODE = 3;
    public static final int BACKNICKNAMECODE = 4;
    public static final String NICKNAME = "backnickname";
    @Bind(R.id.et_nick_name)
    EditText mEtNickName;
    @Bind(R.id.iv_delete_modify_nick)
    ImageView mIvDeleteModifyNick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEtNickName.setFocusable(true);
        nickname = getIntent().getStringExtra("nickname");
        if (nickname.equals("尚未设置昵称")) {
            mEtNickName.setText("");
        } else {
            mEtNickName.setText(nickname);
        }
        initToolsBar();
    }

    private void initToolsBar() {
        setTitle(getString(R.string.modify_nick_name));
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCustomToolbar().addRightButton("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(mEtNickName.getText().toString())){
                    EasyToast.showToast(getApplicationContext(),"昵称不能空白");
                    return;
                }else{
                    Intent intent = getIntent();
                    intent.putExtra(NICKNAME, mEtNickName.getText().toString());
                    setResult(BACKNICKNAMECODE, intent);
                    finish();
                }

            }
        });
        mIvDeleteModifyNick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtNickName.setText("");
            }
        });
        mEtNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    mIvDeleteModifyNick.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_modify_nickname, null);
    }

    public static void start(Activity activty, String nickname) {
        Intent intent = new Intent(activty, ModifyNicknameActivity.class);
        intent.putExtra("nickname", nickname);
        activty.startActivityForResult(intent, NICKNAMECODE);
    }


}
